﻿
var _paq = _paq || [];
$(function () {
    function RegisterModel() {
        this.Score = 0;
        this.RegMethod = 1;//1-phone 2-email
        this.Page = "Register";//Register-正常注册 ；ExclusiveRegister-机构专享注册页;ExclusiveRegisterBySMS-机构专享注册手机
        this.UserRegisterContentPageUrl = "/help-8.html";
        this.RegisterCode = "";
        this.ReturnUrl = "";
        this.From = "";
        this.Register_ad = "";
        this.HomeUrl = "www.benlai.com";
        //tanchuang
        this.PopFlag = "0";
        this.CallBack = function () { };
        this.SuccessFunc = null;
        this.PhoneRegisterOnly = 0;
        this.ImgCodeToken = "";
        this.AliyunRiskRecognition = "0";
        this.showPage = function (popFlag) {
            var that = this;
            if (popFlag && popFlag == "1") {
                this.PopFlag = popFlag;
                ShowBack();
            }
            $.BENLAI.tmpl({
                dom: $("#RegisterTmpl"), v: "url:/Account/Register.html", datas: that, callback: function () {
                    if (that.AliyunRiskRecognition == "1") {
                     pointman.use('do', function () {
                            var afs = pointman.getConfig();
                            var afs_appkey = afs.appkey;
                            var afs_scene = afs.scene;
                            var afs_token = afs.token;
                            document.getElementById('afs_scene').value = afs_scene;
                            document.getElementById('afs_token').value = afs_token;
                     });
                   }
                }
            });
            var dt = new Date();
            dt.setMonth(dt.getMonth() + 1);
            document.cookie = "uuklr=" + this.newGuid() + ";path=/;expires=" + dt.toUTCString();
            return true;
        };
        this.newGuid = function () {
            var guid = "";
            for (var i = 1; i <= 32; i++) {
                var n = Math.floor(Math.random() * 16.0).toString(16);
                guid += n;
                if ((i == 8) || (i == 12) || (i == 16) || (i == 20))
                    guid += "-";
            }
            return guid;
        };
    }

    var overFlag = false;
    var slideFlag = false;
    $("#RegisterTmpl").CreateController({
        elements: {},
        events: {
            "#showPhonePage click": "ShowPagePhone",
            "#showMailPage click": "ShowPageMail",
            "#phoneTxt blur": "phoneValidateAjax",
            "#mailTxt blur":"emailValidateAjax",
            "#smsCodeTxt blur": "smsCodeValidate",
            "#smsCodeBtn click": "getSmsCode",
            "#passTxt blur": "passValidate",
            "#passSureTxt blur": "passSureValidate",
            "#registerBtn click": "submitForm",
            "#imgCodeTxt blur": "imgCodeValidate",
            "#imgCodeBtn click": "GetVryImgGen",
            //laoyonghu
            "#userBRCCodeTxt blur": "userBRCCodeValidate",
            "#userBRCCodeBtn click": "userBRCGetCode",
            "#userBRCBindBtn click": "userBRCBindSub",
            "#userBRCClose click": "userBRCPageHide",
            "#userBindRegisterCode click": "userBRCPageShow",
            //close
            "#registerClose click": "closeRegister",
            //slide
            "#slideBlock mousedown": "slideStart",
            "#slideBlock mouseup": "slideEnd",
            "#phoneImgCodeBtn click": "GetVryClickImgGen",
            "#phoneClickImgCodeImg click": "clickImgCodeSub",
            "#tenWeiboBtn click": "tenWeiboSub"
        },
        slideStart: function () {
            if (overFlag)
                return false;
            slideFlag = true;
            var slideBlock = $("#slideBlock");
            var slideBackground = $("#slideBackground");
            var currentX = slideBlock.offset().left;
            var that = this;
            document.onmouseup = function () {
                slideFlag = false;
                slideBackground.css("width","0px");
                slideBlock.css("left","0px");
            };
            document.onmousemove = function (event) {
                var e = event ? event : window.event;
                if (slideFlag) {
                    var nowX = e.clientX;
                    var disX = nowX - currentX;
                    if (disX <= 0) {
                        slideBlock.css("left", "0px");
                        slideBackground.css("width", "0px");
                    }
                    else if (disX >= 248) {
                        slideBlock.css("left", "248px");
                        slideBackground.css("width", "248px");
                        slideBlock.removeClass("ico01").addClass("ico03");
                        document.onmousemove = function () { };
                        document.onmouseup = function () { };
                        overFlag = true;
                        that.GetVryClickImgGen();
                        $("#clickImgCode").show();
                    }
                    else {
                        slideBlock.css("left", disX + "px");
                        slideBackground.css("width", disX + "px");
                    }
                }
            };

        },
        slideEnd: function () {
            if (!overFlag) {
                slideFlag = false;
                var slideBlock = $("#slideBlock");
                var slideBackground = $("#slideBackground");
                slideBackground.css("width", "0px");
                slideBlock.css("left", "0px");
            }
        },
        GetVryClickImgGen: function () {
            $.BENLAI.ajax({
                type: "get",
                url: "/Account/GetIdentifyCode",
                success: function (result) {
                    if (result) {
                        if (result.error == "0") {
                            var data = result.data;
                            if (data && data.word) {
                                $("#slideWord").html('请点击图中"' + data.word + '"文字');
                                var imageCode = $("#phoneClickImgCodeImg");
                                if (imageCode)
                                    imageCode.attr('src', '/Account/IdentifyCode?' + Math.round(Math.random() * 10000));
                                return true;
                            } else {
                                $("#phoneClickImgCodeMes").html("验证码获取失败，请重试！");
                                return false;
                            }
                        } else {
                            $("#phoneClickImgCodeMes").html("验证码获取失败，请重试！");
                            return false;
                        }
                    } else {
                        $("#phoneClickImgCodeMes").html("验证码获取失败，请重试！");
                        return false;
                    }

                }
            });

        },
        clickImgCodeSub: function (obj,evt) {
            $("#phoneClickImgCodeMes").html("");
            var clickImgCodeImg = $("#phoneClickImgCodeImg");
            var pointImg = clickImgCodeImg.offset();
            var x, y;
            var doc = document.documentElement;
            var body = document.body;
            x = evt.clientX + (doc && doc.scrollLeft || body && body.scrollLeft || 0) - pointImg.left;
            y = evt.clientY + (doc && doc.scrollTop || body && body.scrollTop || 0) - pointImg.top;
            var valueCode = x + "," + y;
            var that = this;
            $.BENLAI.ajax({
                type: "post",
                url: "/Account/CheckIdentifyCode",
                data: { value: valueCode },
                success: function (result) {
                    if (result && result.error == "0") {
                        if ($.BENLAI.RegisterModel && result.data) {
                            $.BENLAI.RegisterModel.ImgCodeToken = result.data;

                            $("#clickImgCode").hide();
                            $("#slideBlock").removeClass("ico03").addClass("ico02");
                            $("#slideWord").html("验证通过！");
                        }
                        else {
                            that.GetVryClickImgGen();
                            $("#phoneClickImgCodeMes").html("验证码获取失败，请重试！");
                            return false;
                        }
                    } else if (result && result.msg) {
                        that.GetVryClickImgGen();
                        $("#phoneClickImgCodeMes").html(result.msg);
                        $("#smsCodeMes").html("");
                        return false;
                    } else {
                        that.GetVryClickImgGen();
                        $("#phoneClickImgCodeMes").html("验证码获取失败，请重试！");
                        return false;
                    }
                }
            });

        },
        init: function () {
            var registerModel = new RegisterModel();
            if(BENLAI.Config && BENLAI.Config.PhoneRegisterOnly)
            {
                registerModel.PhoneRegisterOnly = BENLAI.Config.PhoneRegisterOnly;
            }
            $.BENLAI.RegisterModel = registerModel;
            this.GetVryImgGen();
            this.GetCodeFlag = true;
            this.AccountValidateFlag = true;
            this.focusInit();
        },
        ShowPagePhone: function () {
            var registerModel = $.BENLAI.RegisterModel;
            registerModel.RegMethod = 1;
            registerModel.showPage();
        },
        ShowPageMail: function () {
            var registerModel = $.BENLAI.RegisterModel;
            registerModel.RegMethod = 2;
            registerModel.showPage();
            this.GetVryImgGen();
        },
        focusInit: function () {
            var tmp = $("#RegisterTmpl");

            /*focus*/
            tmp.delegate("#mailTxt", "focus", function () {
                if (this.value == "请输入邮箱") {
                    this.value = "";
                }
            });

            tmp.delegate("#phoneTxt", "focus", function () {
                if (this.value == "请输入手机号") {
                    this.value = "";
                }
            });

            tmp.delegate("#smsCodeTxt", "focus", function () {
                if (this.value == "请输入验证码") {
                    this.value = "";
                }
            });

            tmp.delegate("#imgCodeTxt", "focus", function () {
                if (this.value == "请输入验证码") {
                    this.value = "";
                }
            });

            tmp.delegate("#userBRCCodeTxt", "focus", function () {
                if (this.value == "请输入验证码") {
                    this.value = "";
                }
            });

            /*focus*/
        },
        phoneValidate: function () {
            var type = "phone";
            var txt = document.getElementById(type + "Txt").value;
            if (txt && txt.length > 0) {
                txt = $.trim(txt);
                document.getElementById(type + "Txt").value = txt;
            }
            if (txt.length == 0 || txt == "请输入手机号") {
                document.getElementById(type + "Mes").innerHTML = "请输入手机号";
                var flag = $("dl[data-type=" + type + "]>s");
                flag.removeClass("yes");
                flag.addClass("no");
                return false;
            }
            else {
                var c = /^1[34578][0-9]{9}$/;
                if (!c.test(txt)) {
                    document.getElementById(type + "Mes").innerHTML = "手机号码格式不正确，请重新输入！";
                    var flag = $("dl[data-type=" + type + "]>s");
                    flag.removeClass("yes");
                    flag.addClass("no");
                    return false;
                }
                else {
                    document.getElementById(type + "Mes").innerHTML = "";
                    var flag = $("dl[data-type=" + type + "]>s");
                    flag.removeClass("no");
                    flag.addClass("yes");
                    return true;
                }
            }
        },
        phoneValidateAjax: function () {
            var type = "phone";
            var txt = document.getElementById(type + "Txt").value;
            var register = $.BENLAI.RegisterModel;
            var that = this;
            if (this.phoneValidate()) {
                $.BENLAI.ajax({
                    type: "POST",
                    url: "/Account/ValidatePhone?r=" + Math.round(Math.random() * 10000),
                    data: { phone: txt },
                    async: true,
                    success: function (data) {
                        if (data == 1) {
                            that.AccountValidateFlag = false;
                            if (register.Page == "ExclusiveRegisterBySMS") {
                                $("#userBRCPhone").html(txt);
                                document.getElementById(type + "Mes").innerHTML = '<font color="#eb6100">老用户绑定请点这里<a id="userBindRegisterCode" href="javscript:;"><font  color="black">点击绑定</a></font></font>';
                            } else {
                                document.getElementById(type + "Mes").innerHTML = "该手机号已被使用，可直接<a href='javascript:ShowLoginHeader(1,"+register.PopFlag+");'>登录</a>！";
                            }
                            var flag = $("dl[data-type=" + type + "]>s");
                            flag.removeClass("yes");
                            flag.addClass("no");
                            return false;
                        } else if (data == 0) {
                            that.AccountValidateFlag = true;
                            document.getElementById(type + "Mes").innerHTML = "";
                            var flag = $("dl[data-type=" + type + "]>s");
                            flag.removeClass("no");
                            flag.addClass("yes");
                            return true;
                        } else {
                            that.AccountValidateFlag = false;
                            document.getElementById(type + "Mes").innerHTML = data != null ? data : "";
                            var flag = $("dl[data-type=" + type + "]>s");
                            flag.removeClass("yes");
                            flag.addClass("no");
                            return false;
                        }
                    },
                    error: function () {
                    }
                });
            }
        },
        emailValidate: function () {
            var type = "mail";
            var txt = document.getElementById(type + "Txt").value;
            if (txt && txt.length > 0) {
                txt = $.trim(txt);
                document.getElementById(type + "Txt").value = txt;
            }
            document.getElementById(type + "Mes").innerHTML = "";

            if (txt.length == 0 || txt == "请输入邮箱") {
                document.getElementById(type + "Mes").innerHTML = "请输入邮箱";
                var flag = $("dl[data-type=" + type + "]>s");
                flag.removeClass("yes");
                flag.addClass("no");
                return false;
            }
            else {
                var c = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
                if (!c.test(txt)) {
                    document.getElementById(type + "Mes").innerHTML = "邮箱格式不正确，请重新输入！";
                    var flag = $("dl[data-type=" + type + "]>s");
                    flag.removeClass("yes");
                    flag.addClass("no");
                    return false;
                }
                else {
                    document.getElementById(type + "Mes").innerHTML = "";
                    var flag = $("dl[data-type=" + type + "]>s");
                    flag.removeClass("no");
                    flag.addClass("yes");
                    return true;
                }
            }
        },
        emailValidateAjax: function () {
            var type = "mail";
            var txt = document.getElementById(type + "Txt").value;
            var that = this;
            if (this.emailValidate()) {
                $.BENLAI.ajax({
                    type: "POST",
                    url: "/Account/ValidateEmail?r=" + Math.round(Math.random() * 10000),
                    data: { email: txt },
                    success: function (data) {
                        if (data == 1) {
                            that.AccountValidateFlag = false;
                            document.getElementById(type + "Mes").innerHTML = "该邮箱已被使用，可直接<a href='javascript:ShowLoginHeader(1," + $.BENLAI.RegisterModel.PopFlag + ");'>登录</a>！";
                            var flag = $("dl[data-type=" + type + "]>s");
                            flag.removeClass("yes");
                            flag.addClass("no");
                            return false;
                        } else if (data == 0) {
                            that.AccountValidateFlag = true;
                            document.getElementById(type + "Mes").innerHTML = "";
                            var flag = $("dl[data-type=" + type + "]>s");
                            flag.removeClass("no");
                            flag.addClass("yes");
                            return true;
                        } else {
                            that.AccountValidateFlag = false;
                            document.getElementById(type + "Mes").innerHTML = data;
                            var flag = $("dl[data-type=" + type + "]>s");
                            flag.removeClass("yes");
                            flag.addClass("no");
                            return false;
                        }
                    },
                    error: function () { }
                });
            }
        },
        GetVryImgGen: function () {
            var imageCode = $("#imgCodeImg");
            if (imageCode)
                imageCode.attr('src', '/Account/GetVryImgGen?' + Math.round(Math.random() * 10000));
        },
        imgCodeValidate: function () {
            var type = "imgCode";
            var txt = document.getElementById(type + "Txt").value;
            if (txt.length == 0 || txt == "请输入验证码") {
                document.getElementById(type + "Mes").innerHTML = "请输入验证码!";
                return false;
            }
            else {
                document.getElementById(type + "Mes").innerHTML = "";
                return true;
            }
        },
        smsCodeValidate: function () {
            var type = "smsCode";
            var txt = document.getElementById(type + "Txt").value;
            if (txt.length == 0 || txt == "请输入验证码") {
                document.getElementById(type + "Mes").innerHTML = "请输入验证码!";
                return false;
            }
            else {
                document.getElementById(type + "Mes").innerHTML = "";
                return true;
            }
        },
        passValidate: function () {
            var type = "pass";
            var txt = document.getElementById(type + "Txt").value;
            document.getElementById(type + "Mes").innerHTML = "";
            var passPat = /^(?![0-9]+$)(?![a-zA-Z]+$)[\w\S]{8,16}$/;
            if (!passPat.test(txt)) {
                document.getElementById(type + "Mes").innerHTML = "密码须为8-16位的字母与数字组合!";
                var flag = $("dl[data-type=" + type + "]>s");
                flag.removeClass("yes");
                flag.addClass("no");
                return false;
            }
            if (txt.length == 0 || txt == "请输入密码") {
                document.getElementById(type + "Mes").innerHTML = "请输入密码!";
                var flag = $("dl[data-type=" + type + "]>s");
                flag.removeClass("yes");
                flag.addClass("no");
                return false;
            }
            else if (txt.length < 8 || txt.length > 16) {
                document.getElementById(type + "Mes").innerHTML = "密码至少8位，最多16位!";
                var flag = $("dl[data-type=" + type + "]>s");
                flag.removeClass("yes");
                flag.addClass("no");
                return false;
            }
            else {
                document.getElementById(type + "Mes").innerHTML = "";
                var flag = $("dl[data-type=" + type + "]>s");
                flag.removeClass("no");
                flag.addClass("yes");
                return true;
            }
        },
        passSureValidate: function () {

            var type = "passSure";
            var txt = document.getElementById(type + "Txt").value;
            var txtPass = document.getElementById("passTxt").value;
            document.getElementById(type + "Mes").innerHTML = "";
            if (txt.length == 0 || txt == "请输入确认密码") {
                document.getElementById(type + "Mes").innerHTML = "请输入确认密码!";
                var flag = $("dl[data-type=" + type + "]>s");
                flag.removeClass("yes");
                flag.addClass("no");
                return false;
            }
            else if (this.passValidate()) {
                if (txt != txtPass) {
                    document.getElementById(type + "Mes").innerHTML = "两次输入的密码不一致!";
                    var flag = $("dl[data-type=" + type + "]>s");
                    flag.removeClass("yes");
                    flag.addClass("no");
                    return false;
                }
                else {
                    document.getElementById(type + "Mes").innerHTML = "";
                    var flag = $("dl[data-type=" + type + "]>s");
                    flag.removeClass("no");
                    flag.addClass("yes");
                    return true;
                }
            }
        },
        getSmsCode: function () {
            if (this.AccountValidateFlag) {
                var type = "phone";
                var txt = document.getElementById(type + "Txt").value;
                var register = $.BENLAI.RegisterModel;
                var that = this;
                if (that.phoneValidate()) {
                    if (that.GetCodeFlag) {
                        var url = "/Account/GetRegisterSMSCode?r=" + Math.round(Math.random() * 10000);
                        var phone = document.getElementById("phoneTxt").value;
                        var imgCodeToken = "";
                        if ($.BENLAI.RegisterModel.ImgCodeToken)
                            imgCodeToken = $.BENLAI.RegisterModel.ImgCodeToken;
                        $.BENLAI.ajax({
                            type: "POST",
                            url: url,
                            data: { cellphone: phone,imgCodeToken:imgCodeToken },
                            success: function (data) {
                                if (data.error != "0") {
                                    var errorMes = data.msg;
                                    if (data.error == "2") {
                                        if (register.Page == "ExclusiveRegisterBySMS") {
                                            $("#userBRCPhone").html(txt);
                                            errorMes = '<font color="#eb6100">老用户绑定请点这里<a id="userBindRegisterCode" href="javscript:;"><font  color="black">点击绑定</a></font></font>';
                                        } else {
                                            errorMes = "该手机号已被使用，可直接<a href='javascript:ShowLoginHeader(1," + register.PopFlag + ");'>登录</a>！";
                                        }
                                    }
                                    if (data.error == "3" || data.error == "4") {
                                        document.getElementById("smsCodeMes").innerHTML = errorMes;
                                    } 
                                    else {
                                        document.getElementById(type + "Mes").innerHTML = errorMes;
                                        var flag = $("dl[data-type=" + type + "]>s");
                                        flag.removeClass("yes");
                                        flag.addClass("no");
                                    }
                                }
                                else {
                                    var btn = $("#smsCodeBtn");
                                    btn.addClass("phone_none");
                                    btn.html('59秒后重新获取');
                                    that.GetCodeFlag = false;

                                    var now = new Date();
                                    var date = new Date(now.getTime() + 60000);
                                    $.BENLAI.countDown({
                                        dom: btn,
                                        date: date,
                                        temp: '{second}秒后重新获取',
                                        callback: function () {
                                            that.GetCodeFlag = true;
                                            btn.removeClass('phone_none');
                                            btn.html('重新获取');
                                        }
                                    });

                                    document.getElementById(type + "Mes").innerHTML = "";
                                    var flag = $("dl[data-type=" + type + "]>s");
                                    flag.removeClass("no");
                                    flag.addClass("yes");
                                }
                            }
                        });
                    }
                }
            }
        },
        submitForm: function () {
            if (this.AccountValidateFlag) {
                var registerModel = $.BENLAI.RegisterModel;
                var popFlag = false;
                if (registerModel && registerModel.PopFlag && registerModel.PopFlag == "1") {
                    popFlag = true;
                }

                if ($("#checkboxUserRegisterContentPage").attr("checked") == false) {
                    if (popFlag) {
                        $("#passSureMes").html("请先同意本来生活网 服务条款");
                    }
                    else
                        alert("请先同意本来生活网 服务条款", 1);
                    $("#checkboxUserRegisterContent").focus();
                    return false
                }
                var flag = false;
                var regType = 2;
                if (registerModel.RegMethod == 2) {
                    if (this.emailValidate() && this.passSureValidate() && this.imgCodeValidate()) {
                        flag = true;
                        regType = 2;
                    }
                }
                else {
                    if (this.phoneValidate() && this.smsCodeValidate() && this.passSureValidate()) {
                        flag = true;
                        regType = 1;
                    }
                }
                if (flag) {
                    var that = this;
                    $.BENLAI.ajax({
                        type: "POST",
                        url: "/Account/Register?r=" + Math.round(Math.random() * 10000),
                        data: $("#formReg").serialize(),
                        cache: false,
                        success: function (result) {
                            if (regType == 2) {
                                that.GetVryImgGen();
                            }
                            if (AjaxValidate(result)) {
                                if (result.error == "0") {
                                    try {
                                        _paq.push(['trackEvent', '用户', '注册', 'Web注册', 1]);
                                        _paq.push(['trackPageView']);
                                    }
                                    catch (e) { }
                                    var data = result.data;
                                    //bind
                                    var bindPhoneModel = $.BENLAI.BindPhoneModel;
                                    bindPhoneModel.SuccessMes = "恭喜您注册成功!";
                                    var retrurnUrl = data ? data.ReturnUrl : "";
                                     
                                    if (popFlag) {
                                        that.hideRegister();
                                        bindPhoneModel.CallBack = function () {
                                            if (registerModel.SuccessFunc) {
                                                that.closeRegister();
                                                registerModel.SuccessFunc();
                                            } else {
                                                if (retrurnUrl) {
                                                    window.location.href = decodeURIComponent(retrurnUrl);
                                                    return false;
                                                } else {
                                                    window.location.href = getLocationHref("/");
                                                    return false;
                                                }
                                            }
                                        }
                                    } else {
                                        bindPhoneModel.CallBack = function () {
                                            if (registerModel.SuccessFunc) {
                                                that.closeRegister();
                                                registerModel.SuccessFunc();
                                            } else {
                                                if (retrurnUrl) {
                                                    window.location.href = decodeURIComponent(retrurnUrl);
                                                    return false;
                                                } else {
                                                    window.location.href = getLocationHref("/");
                                                    return false;
                                                }
                                            }
                                        }
                                    }
                                    that.RegisterSuccess(data, popFlag);
                                } else {
                                    if (popFlag) {
                                        if (result.error == "2") {
                                            if (regType == 2) {
                                                $("#imgCodeMes").html(result.msg);
                                            } else {
                                                $("#smsCodeMes").html(result.msg);
                                            }

                                        } else if (result.error == "3") {
                                            if (regType == 2) {
                                                $("#mailMes").html("该邮箱已被使用，可直接<a href='javascript:ShowLoginHeader(1," + $.BENLAI.RegisterModel.PopFlag + ");'>登录</a>！");
                                            } else {
                                                $("#phoneMes").html("该手机号已被使用，可直接<a href='javascript:ShowLoginHeader(1," + $.BENLAI.RegisterModel.PopFlag + ");'>登录</a>！");
                                            }
                                        } else {
                                            $("#passSureMes").html(result.msg);
                                        }
                                    }
                                    else {
                                        alert(result.msg);
                                    }
                                }
                            }
                        },
                        error: function () {
                            if (popFlag) {
                                that.hideRegister();
                            }
                            alert("注册失败，请重试", 1);
                        }
                    });
                }
            }
        },
        //老用户绑定
        userBRCPageShow: function () {
            ShowBack();
            $("#userBindRegCodeDiv").show();
        },
        userBRCPageHide: function () {
            HideBack();
            $("#userBindRegCodeDiv").hide();
        },
        userBRCCodeValidate: function () {
            var type = "userBRCCode";
            var txt = document.getElementById(type + "Txt").value;
            if (txt.length == 0 || txt == "请输入验证码") {
                document.getElementById(type + "Mes").innerHTML = "请输入验证码!";
                return false;
            }
            else {
                document.getElementById(type + "Mes").innerHTML = "";
                return true;
            }
        },
        userBRCGetCode: function () {
            var that = this;
            var phone = $("#userBRCPhone").html();
            var c = /^1[34578][0-9]{9}$/;
            if (phone.length > 0 && c.test(phone)) {
                if (that.GetCodeFlag) {
                    $.BENLAI.ajax({
                        type: "POST",
                        url: "/Account/SendSMSBindRegisterCode",
                        data: { cellphone: phone },
                        success: function (result) {
                            if (result.error != "0") {
                                document.getElementById("userBRCCodeMes").innerHTML = result.msg;
                            }
                            else {
                                var btn = $("#userBRCCodeBtn");
                                btn.addClass("phone_none");
                                btn.html('59秒后重新获取');
                                that.GetCodeFlag = false;
                                var now = new Date();
                                var date = new Date(now.getTime() + 60000);
                                $.BENLAI.countDown({
                                    dom: btn,
                                    date: date,
                                    temp: '{second}秒后重新获取',
                                    callback: function () {
                                        that.GetCodeFlag = true;
                                        btn.removeClass('phone_none');
                                        btn.html('重新获取');
                                    }
                                });
                            }

                        },
                        error: function (e) {
                            document.getElementById("userBRCCodeMes").innerHTML ="发送验证码失败，请重试！";
                        }
                    });
                }
            }
            else {
                document.getElementById("userBRCCodeMes").innerHTML = "手机号有误!";
            }
        },
        userBRCBindSub: function () {
            var that = this;
            if (that.userBRCCodeValidate()) {
                var phone = $("#userBRCPhone").html();
                var registerCode = $("#registerCode").val();
                var validatingCode = $("#userBRCCodeTxt").val();
                $.BENLAI.ajax({
                    type: "POST",
                    url: "/Account/CheckShow",
                    data: "phone=" + phone + "&registerCode=" + registerCode + "&validatingCode=" + validatingCode,
                    cache: false,
                    success: function (data) {
                        if (AjaxValidate(data)) {
                            if (data.status == 1) {
                                that.userBRCPageHide();
                                alert("您已是会员，本次活动指定粽子的优惠券已绑定到您账户，购物结算时可直接抵用，同时您手中的粽子抵用券自动作废。", 1);
                                setTimeout(function () {
                                    window.location.href = '/UserHome/User_Quan';
                                }, 3000);
                            } else if (data.status == -1) {
                                document.getElementById("userBRCCodeMes").innerHTML =data.message;
                            }

                        } else {
                            document.getElementById("userBRCCodeMes").innerHTML = data;
                        }
                    },
                    error: function () {
                        document.getElementById("userBRCCodeMes").innerHTML = "优惠券绑定失败，请重试";
                    }
                })
            }
        },
        closeRegister: function () {
            if ($.BENLAI.RegisterModel.PopFlag == "1")
            {
                HideBack();
            }
            $("#RegisterTmpl").html("");
            $.BENLAI.RegisterModel.CallBack();
            $.BENLAI.RegisterModel = null;
        },
        hideRegister: function ()
        {
            if ($.BENLAI.RegisterModel.PopFlag == "1") {
                HideBack();
            }
            $("#registerDiv").hide();
        },
        RegisterSuccess: function (data, popFlag) {
            if (data && data.BindPhoneTipType) {
                var bindPhoneModel = $.BENLAI.BindPhoneModel;
                if (data.LoginBranchType && data.LoginBranchType != 1) {
                    bindPhoneModel.NeedPass = true;
                }
                var BindPhoneTipType = data.BindPhoneTipType;
                if (popFlag) {
                    this.hideRegister();
                }
                if (BindPhoneTipType == 1) {
                    bindPhoneModel.CallBack();
                } else if (BindPhoneTipType == 2) {//must2
                    bindPhoneModel.SuccessMesFlag = true;
                    bindPhoneModel.SuccessMes = "注册成功，绑定手机即可登录！";
                    bindPhoneModel.NeverTipFlag = false;
                    bindPhoneModel.MustBindFlag = 1;
                    bindPhoneModel.FirstBindFlag = data.BindPhoneFirstFlag;
                    bindPhoneModel.Score = data.Score > 0 ? data.Score : 0;
                    bindPhoneModel.CusToken = data.CusToken;
                    bindPhoneModel.Account = data.CustomerID;
                    bindPhoneModel.PopFlag = true;
                    bindPhoneModel.showPage();
                } else if (BindPhoneTipType == 3) {
                    var bindPhoneModel = $.BENLAI.BindPhoneModel;
                    bindPhoneModel.SuccessMesFlag = true;
                    bindPhoneModel.NeverTipFlag = true;
                    bindPhoneModel.FirstBindFlag = data.BindPhoneFirstFlag;
                    bindPhoneModel.Score = data.Score > 0 ? data.Score : 0;
                    bindPhoneModel.PopFlag = true;
                    bindPhoneModel.showPage();
                }
            }
            DSPGoForRegisterSuccess(data.CustomerID)
        },
        tenWeiboSub: function () {
            var that = this;
            T.login(function (loginStatus) {
                that.getUserInfo();
            }, function (loginError) {
                //alert(loginError.message);
            });
        },
        getUserInfo: function () {
            var that = this;
            T.api("/user/info")
           .success(function (response) {
               if (response.ret === 0) {
                   var data = response.data;
                   //获取信息就退出登录
                   T.logout();
                   //用户数据 
                   $.ajax({
                       type: "POST",
                       url: "/Account/TencentWeibo_Login",
                       async: false,
                       cache: false,
                       data: {
                           openid: data.openid, sex: data.sex, nick: data.nick
                       },
                       dataType: "json",
                       success: function (result) {
                           if (AjaxValidate(result)) {
                               if (result.error == "0") {
                                   var bindPhoneModel = $.BENLAI.BindPhoneModel;
                                   var retrurnUrl = result.data ? result.data.ReturnUrl : "";
                                   var popFlag = 0;
                                   if (popFlag) {
                                       bindPhoneModel.CallBack = function () {
                                           if (loginModel.SuccessFunc) {
                                               that.closeLogin();
                                               loginModel.SuccessFunc();
                                           }
                                           else {
                                               if (retrurnUrl) {
                                                   window.location.href = decodeURIComponent(retrurnUrl);
                                                   return false;
                                               } else {
                                                   window.location.href = getLocationHref("/");
                                                   return false;
                                               }
                                           }
                                           return false;
                                       };
                                   }
                                   else {
                                       bindPhoneModel.CallBack = function () {
                                           if (retrurnUrl) {
                                               window.location.href = decodeURIComponent(retrurnUrl);
                                               return false;
                                           } else {
                                               window.location.href = getLocationHref("/");
                                               return false;
                                           }
                                       };
                                   }
                                   that.LoginSuccess(result.data);
                               }
                               else {
                                   if (result.msg) {
                                       alert(result.msg);
                                   }
                               }
                           }
                       }
                   })
               } else {
                   //alert(response.ret);
               }
           })
          .error(function (code, message) {
              //alert(message);
          });
        },
        LoginSuccess: function (data, popFlag) {
            if (data && data.BindPhoneTipType) {
                var bindPhoneModel = $.BENLAI.BindPhoneModel;
                if (data.LoginBranchType && data.LoginBranchType != 1) {
                    bindPhoneModel.NeedPass = true;
                }
                var BindPhoneTipType = data.BindPhoneTipType;
                if (popFlag) {
                    this.hideLogin();
                }
                if (BindPhoneTipType == 1) {
                    bindPhoneModel.CallBack();
                } else if (BindPhoneTipType == 2) {//must2
                    bindPhoneModel.SuccessMesFlag = true;
                    bindPhoneModel.SuccessMes = "您需要先进行手机绑定！";
                    bindPhoneModel.NeverTipFlag = false;
                    bindPhoneModel.MustBindFlag = 1;
                    bindPhoneModel.FirstBindFlag = data.BindPhoneFirstFlag;
                    bindPhoneModel.Score = data.Score > 0 ? data.Score : 0;
                    bindPhoneModel.CusToken = data.CusToken;
                    bindPhoneModel.Account = data.CustomerID;
                    bindPhoneModel.PopFlag = true;

                    bindPhoneModel.showPage();
                } else if (BindPhoneTipType == 3) {
                    var bindPhoneModel = $.BENLAI.BindPhoneModel;
                    bindPhoneModel.SuccessMesFlag = true;
                    bindPhoneModel.SuccessMes = "您已登陆成功！";
                    bindPhoneModel.NeverTipFlag = true;
                    bindPhoneModel.FirstBindFlag = data.BindPhoneFirstFlag;
                    bindPhoneModel.Score = data.Score > 0 ? data.Score : 0;
                    bindPhoneModel.PopFlag = true;
                    bindPhoneModel.showPage();
                }
            }
        }
    });

})
